using System;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
using System.Globalization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Havivi.ScrumSharePointTemplate
{
    public class ScrumListsTemplateReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties.Feature.Parent is SPWeb)
            {

                SPWeb web = (SPWeb)properties.Feature.Parent;

                if (!ListExists(web, Constants.SprintBacklog))
                {
                    Guid guid = web.Lists.Add(Constants.SprintBacklog,
                                           "The Sprint Backlog is the set of tasks planned for implementation in a Sprint",
                                           SPListTemplateType.Tasks);
                    SPList list = web.Lists[Constants.SprintBacklog];
                    string id = list.Fields.Add("ScrumTaskBoardColumn", SPFieldType.Text, false);
                    SPField field = list.Fields[id];
                    field.DefaultValue = "0";
                    if (field.CanToggleHidden)
                    {
                        field.Hidden = true;
                    }
                    field.Update();
                    id = list.Fields.Add("ScrumTaskBoardID", SPFieldType.Text, false);
                    field = list.Fields[id];

                    if (field.CanToggleHidden)
                    {
                        field.Hidden = true;
                    }
                    field.Update();
                    list.OnQuickLaunch = true;
                    list.Update();
                    web.Update();
                }
                if (!ListExists(web, Constants.ScrumTaskBoardColumns))
                {
                    Guid guid = web.Lists.Add(Constants.ScrumTaskBoardColumns,
                                           "This list define the Scrum Task Board columns.",
                                           SPListTemplateType.GenericList);
                    SPList list = web.Lists[Constants.ScrumTaskBoardColumns];
                    list.OnQuickLaunch = false;
                    web.AllowUnsafeUpdates = true;
                    list.Update();
                    web.Update();
                }

                try
                {
                    SPList listColumns = web.Lists[Constants.ScrumTaskBoardColumns];
                    if (listColumns.ItemCount == 0)
                    {
                        SPListItem item = listColumns.Items.Add();
                        item["Title"] = "To Do";
                        item.Update();
                        item = listColumns.Items.Add();
                        item["Title"] = "In Process";
                        item.Update();
                        item = listColumns.Items.Add();
                        item["Title"] = "To Verify";
                        item.Update();
                        item = listColumns.Items.Add();
                        item["Title"] = "Done";
                        item.Update();
                    }
                }
                catch
                {
                    //error in demo data - do nothing
                }

            }


        }

        public static bool ListExists(SPWeb web, string listName)
        {
            return web.Lists.Cast<SPList>().Any(list => string.Equals(list.Title, listName));
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            if (properties.Feature.Parent is SPSite)
            {
                SPSite site = (SPSite)properties.Feature.Parent;
                List<string> webparts = new List<string>();
                SPElementDefinitionCollection elementColletion = properties.Definition.GetElementDefinitions(CultureInfo.CurrentCulture);
                foreach (SPElementDefinition element in elementColletion)
                {
                    foreach (XmlElement xmlNode in element.XmlDefinition.ChildNodes)
                    {
                        if (xmlNode.Name.Equals("File"))
                        {
                            webparts.Add(xmlNode.Attributes["Url"].Value);
                        }
                    }
                }


                SPList wpGallery = site.RootWeb.GetCatalog(SPListTemplateType.WebPartCatalog);

                List<SPListItem> items = new List<SPListItem>();
                foreach (SPListItem item in wpGallery.Items)
                {
                    if (webparts.Contains(item.File.Name))
                    {
                        items.Add(item);
                    }
                }

                foreach (SPListItem item in items)
                {
                    item.Delete();
                }
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        { }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

    }
}
